package com.android.wellcherish.workclock;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.android.wellcherish.workclock.bean.data.GroupRecord;
import com.android.wellcherish.workclock.bean.db.Record;
import com.android.wellcherish.workclock.database.AppDatabase;
import com.android.wellcherish.workclock.database.DbTaskManager;
import com.android.wellcherish.workclock.util.CommonUtil;
import com.android.wellcherish.workclock.util.TimeUtil;
import com.android.wellcherish.workclock.view.LineItemDecoration;
import com.android.wellcherish.workclock.view.RecordAdapter;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangwenchen
 * @date 2019/7/15
 * */
public class RecordsActivity extends AppCompatActivity {
    private AppDatabase database;
    private DbTaskManager manager;

    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_records);

        rv = findViewById(R.id.rv);

        initData();
    }

    private void initData() {
        database = AppDatabase.getInstance(this);
        manager = new DbTaskManager(this, database);

        List<Record> records = manager.queryAll();

        if (CommonUtil.isEmptyList(records)) {
            return;
        }

        List<GroupRecord> groupRecords = CommonUtil.getGroupRecords(records, true);

        RecordAdapter adapter = new RecordAdapter(this, groupRecords);
        adapter.setCanFold(true);
        rv.addItemDecoration(new LineItemDecoration(CommonUtil.dpToPx(this, 1),
            CommonUtil.dpToPx(this, 40), CommonUtil.dpToPx(this, 0)));
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);
    }

    @Override protected void onDestroy() {
        super.onDestroy();

        database = null;
        manager.cancel();
        manager = null;
    }
}
